﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Base.Helper
{
    public class GetHelper
    {
        /// <summary>
        /// 获取int值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int GetInt(object obj)
        {
            if (obj == null) return 0;
            return GetInt(obj.ToString());
        }

        /// <summary>
        /// 获取int值
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static int GetInt(string text)
        {
            if (string.IsNullOrEmpty(text)) return 0;
            text = text.Trim();
            int outResult;

            Int32.TryParse(text, out outResult);
            return outResult;
        }
        /// <summary>
        /// 获取decimal值
        /// </summary>
        public static decimal GetDecimal(string text)
        {
            if (string.IsNullOrEmpty(text)) return 0;
            text = text.Trim();
            decimal outResult;
            Decimal.TryParse(text, out outResult);
            return outResult;
        }

        /// <summary>
        /// 获取DateTime值
        /// </summary>
        public static DateTime GetDateTime(string text)
        {
            if (string.IsNullOrEmpty(text)) return DateTime.MinValue;
            text = text.Trim();
            DateTime outResult;
            DateTime.TryParse(text, out outResult);
            return outResult;
        }
        /// <summary>
        /// 获取string值  替换 ' => ''
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string GetStr(object obj)
        {
            return GetStr(obj, false);
        }
        /// <summary>
        /// 获取string值
        /// </summary>
        /// <param name="text"></param>
        /// <param name="replace">是否替换 ' => ''</param>
        /// <returns></returns>
        public static string GetStr(object obj, bool replace)
        {
            if (obj == null || obj == DBNull.Value)
                return string.Empty;
            string text = obj.ToString();

            text = string.IsNullOrEmpty(text) ? string.Empty : text.Trim();
            return replace == true ? text.Replace("'", "''") : text;
        }
        /// <summary>
        /// 获取0-9 a-z的随机字符串
        /// </summary>
        /// <param name="intCount">n位随机字符串</param>
        /// <returns></returns>
        public static string GetRandomStr(int intCount)
        {
            string strReturn = "";
            if (intCount > 0)
            {
                Random random = new Random();
                string[] strArray = new string[36] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

                for (int i = 0; i < intCount; i++)
                {
                    strReturn += strArray[random.Next(0, 35)];
                }
            }
            return strReturn;
        }


        /// <summary>
        /// 获取0-9的随机数
        /// </summary>
        /// <param name="intCount">n位随机随机数</param>
        /// <returns></returns>
        public static string GetRandomNumber(int intCount)
        {
            string strReturn = "";
            if (intCount > 0)
            {
                Random random = new Random();
                string[] strArray = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

                for (int i = 0; i < intCount; i++)
                {
                    strReturn += strArray[random.Next(0, 10)];
                }
            }
            return strReturn;
        }
        /// <summary>
        /// 获取1-9的随机数
        /// </summary>
        /// <param name="intCount">n位随机随机数</param>
        /// <returns></returns>
        public static string GetRandomNumberNoZero(int intCount)
        {
            string strReturn = "";
            if (intCount > 0)
            {
                Random random = new Random();
                string[] strArray = new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };

                for (int i = 0; i < intCount; i++)
                {
                    strReturn += strArray[random.Next(0, 9)];
                }
            }
            return strReturn;
        }


        /// 时间戳转成时间类型
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }
        /// <summary>
        /// 时间类型转成long
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long ConvertDateTimeInt(System.DateTime time)
        {
            long intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            intResult = (long)(time - startTime).TotalSeconds;
            return intResult;
        }
        /// <summary>
        /// 输出json信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Dictionary<string, object> dic_output_data(object data, string msg = "", bool b = true)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("status", b == true ? "success" : "fail");
            dic.Add("msg", msg);
            dic.Add("data", data);
            return dic;
        }

        /// <summary>
        /// 输出json信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Dictionary<string, object> dic_output_data(object data, int totalPage, string msg = "", bool b = true)
        {
            Dictionary<string, object> dict = dic_output_data(data, msg, b);
            dict.Add("TotalPage", totalPage);
            return dict;
        }

    }


}
